<?php
namespace Cyfy;

/** 
 * Loading modules.
 *
 * Class used for loading modules, their language files, constructors and desctructors.
 * @category cyfy
 * @author Dandelion <dandelion8888@gmail.com>
 * @version 0.1
 */
class Modules
{
    /**
     * Enabled modules.
     * @var array
     */
    static private $modules = array();

    /**
     * Enabled modules names.
     * @var array
     */
    static public $modulesNames = array();
    
    /**
     * Installed modules.
     * @var array
     */
    static private $allModules = array();
   
    
    static private $always = array();
    
    /**
     * Loads modules from DB.
     * This method is called when cyfy starts.
     */
    static public function prepare()
    {
        // Loading modules
       
        $result = \dibi :: query("SELECT [name],[state], [always] FROM [:cyfy:Modules] ORDER BY [mid] ASC");
        foreach($result as $m)
        {
            self :: $modulesNames[$m["name"]] = $m["name"];
            self :: $allModules[] = $m["name"];
            
            if($m["state"] == 2){
                self :: $modules[] = $m["name"];
            }
            
            if($m["always"] == 1){
                self :: $always[] = $m["name"];
            }
        }
        
      
        // sorts names
      //  asort(self :: $modulesNames);
    }
    
    /**
     * Loads the language files
     * This method is called when cyfy starts.     
     */
    static public function loadLanguageFiles()
    {
     
        // sorts names
        asort(self :: $modulesNames);
    }    
    
    /**
     * Loads the start functions for modules.
     * This method is called when cyfy starts.     
     */
    static public function loadStarters()
    {
        foreach(self :: $always as $module)
        {
            $class = "\\Cyfy\Modules\\$module\Module";
            if(file_exists("Cyfy/Modules/$module/Module.php"))
            {
                if(method_exists($class, "start"))
                {
                    $class :: start();
                    \Cyfy\Log :: write("Module $module started.");
                }
            }
        }
    }
    
    /**
     * Loads the end functions for modules.
     * This method is called when cyfy ends.     
     */
    static public function loadEnders()
    {
        foreach(self :: $always as $module)
        {
            $class = "\Cyfy\Modules\\$module\Module";
            if(file_exists("Cyfy/Modules/$module/Module.php"))
            {
                if(method_exists($class, "end"))
                {
                    $class :: end();
                    \Cyfy\Log :: write("Module $module ended.");
                }
            }
        }
    }

    /**
     * Returns enabled modules.
     * @return array Enabled modules
     */
    static public function getEnabled()
    {
        return self :: $modules;
    }
    
    /**
     * Returns all installed (running & disabled) modules.
     * @return array Installed modules
     */
    static public function getInstalled()
    {
        return self :: $allModules;
    }
    
    
    /**
     * Checks, if a module is enabled.
     * @param string Module name.
     * @return bool TRUE, if module is enabled. FALSE if not.
     */
    static public function isEnabled($module)
    {
        return in_array($module, self :: $modules);
    }
    
    
    /**
     * Gets module info from XML file.
     *
     * @param string Class name and directory name, where the module is stored.
     * @return mixed Associative array with information about the module or FALSE if not found.
     */
    static function getInfo($name)
    {
        $filename = "Cyfy/Modules/" . $name . "/info.xml";
        if(!file_exists($filename)){
            return false;
        }
        
        $module = simplexml_load_file($filename);
		$info['name'] = (string) $module -> name;
		$info['classname'] = $name;
		$info['project'] = (string)$module -> project;
        $info['version'] = (string) $module -> version;
        $info['description'] = (string) $module -> description;
        $info['author'] = (string) $module ->  author;
        $info['name'] = $name;
        
        $info['dependencies'] = array();
        if($module -> dependencies){
            foreach($module -> dependencies -> dependency as $dependency){
                $info['dependencies'][] = (string) $dependency;
            }
        }
        
        $info['permissions'] = array();
        if($module -> permissions){
            foreach($module -> permissions -> permission as $permission){
                 $info['permissions'][] = (string) $permission;
            }
        }
  
        return $info;
    }
}
